package com.ruoyi.shop.systematic.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.systematic.admin.vo.adminvo;
import com.ruoyi.mall.common.domain.DtsRole;
import com.ruoyi.mall.common.mapper.DtsRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.common.mapper.DtsAdminMapper;
import com.ruoyi.mall.common.domain.DtsAdmin;
import com.ruoyi.shop.systematic.admin.service.IDtsAdminService;

/**
 * 管理员Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class DtsAdminServiceImpl implements IDtsAdminService
{
    @Autowired
    private DtsAdminMapper dtsAdminMapper;
    @Autowired
    private DtsRoleMapper dtsRoleMapper;


    @Override
    public List<adminvo> selectDtsAdminList(DtsAdmin user) {
        List<DtsAdmin> userList = dtsAdminMapper.selectDtsAdminList(user);
        List<adminvo> userVOList = new ArrayList<>();

        for (DtsAdmin sysUser : userList) {
            adminvo userVO = new adminvo();
            userVO.setId(sysUser.getId());
            userVO.setUsername(sysUser.getUsername());
            userVO.setAvatar(sysUser.getAvatar());

            // 获取角色名称
            String roleIds = sysUser.getRoleIds().replace("[", "").replace("]", "");
            List<DtsRole> roles = dtsRoleMapper.selectRolesByIds(roleIds);
            String roleNames = roles.stream().map(DtsRole::getName).collect(Collectors.joining(","));
            userVO.setRoleNames(roleNames);

            userVOList.add(userVO);
        }

        return userVOList;
    }

    @Override
    public List<DtsRole> selectDtsRoleList() {
        return dtsAdminMapper.selectDtsRoleList();
    }

    /**
     * 查询管理员
     *
     * @param id 管理员主键
     * @return 管理员
     */
    @Override
    public DtsAdmin selectDtsAdminById(Long id)
    {

        return dtsAdminMapper.selectDtsAdminById(id);
    }

    /**
     * 查询管理员列表
     *
     * @param dtsAdmin 管理员
     * @return 管理员
     */
    //@Override
//    public List<DtsAdmin> selectDtsAdminList(DtsAdmin dtsAdmin)
//    {
//        return dtsAdminMapper.selectDtsAdminList(dtsAdmin);
//    }

    /**
     * 新增管理员
     *
     * @param dtsAdmin 管理员
     * @return 结果
     */
    @Override
    public int insertDtsAdmin(DtsAdmin dtsAdmin)
    {
        return dtsAdminMapper.insertDtsAdmin(dtsAdmin);
    }

    /**
     * 修改管理员
     *
     * @param dtsAdmin 管理员
     * @return 结果
     */
    @Override
    public int updateDtsAdmin(DtsAdmin dtsAdmin)
    {
        dtsAdmin.setUpdateTime(DateUtils.getNowDate());
        return dtsAdminMapper.updateDtsAdmin(dtsAdmin);
    }

    /**
     * 批量删除管理员
     *
     * @param ids 需要删除的管理员主键
     * @return 结果
     */
    @Override
    public int deleteDtsAdminByIds(Long[] ids)
    {
        return dtsAdminMapper.deleteDtsAdminByIds(ids);
    }

    /**
     * 删除管理员信息
     *
     * @param id 管理员主键
     * @return 结果
     */
    @Override
    public int deleteDtsAdminById(Long id)
    {
        return dtsAdminMapper.deleteDtsAdminById(id);
    }
}
